package cn.sumpu.app.coolsk.biz.impl;

import java.util.*;
//import org.slf4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import cn.sumpu.app.coolsk.biz.*;
import cn.sumpu.app.coolsk.constant.Table;
import cn.sumpu.app.coolsk.dao.*;
import cn.sumpu.app.coolsk.domain.*;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.page.PageModel;
import cn.sumpu.toolkit.common.util.Common;

public class InfoBizImpl implements InfoBiz {

//	private final static Logger logger = LoggerFactory
//			.getLogger(InfoBizImpl.class);

	@Autowired
	private IdentifierBiz identifierBiz;
	
	private InfoDao infoDao;
	
	public void setInfoDao(InfoDao infoDao) {
		this.infoDao = infoDao;
	}

	/**
	 * author BruceZhang
	 * @param info
	 * 添加信息
	 * @throws AppBusinessException
	 */
	@Override
	public Info addInfo(Info info) throws AppBusinessException {
		if(Common.emptyStr(info.getId())){
			info.setId(identifierBiz.generateId(Table.INFO));
		}
		if(Common.emptyStr(info.getHyperlink())){
			info.setHyperlink("infoDetail.htm?id="+info.getId());
		}
		infoDao.addInfo(info);
		return info;
	}

	/**
	 * author BruceZhang
	 * @param title			标题
	 * @return	查询信息
	 * @throws AppBusinessException
	 */
	@Override
	public PageModel getInfo(String title, Integer pageSize, Integer page)
			throws AppBusinessException {
		if(page==null || page<1){
			page = 1;
		}
		if(pageSize==null || pageSize<1){
			pageSize = 10;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if (!Common.empty(title)) {
            map.put("title", "%" + title + "%");
        }
		map.put("pageSize", pageSize);
		map.put("startId", (page - 1) * pageSize);
		List<Info> infoList = infoDao.getInfo(map);
		Integer count = getInfoCount(title);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(infoList);
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1) / pageSize);
		return pm;
	}
	//查询信息总条数
	@Override
	public Integer getInfoCount(String title)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", title);
		return infoDao.getInfoCount(map);
	}
	//删除信息
	@Override
	public void deleteInfo(String id) throws AppBusinessException {
		infoDao.deleteInfo(id);
	}
	//更新信息
	@Override
	public void updateInfo(Info info) throws AppBusinessException {
		infoDao.updateInfo(info);
	}
	//通过ID取得某条信息
	@Override
	public Info getInfoById(String id) throws AppBusinessException {
		return infoDao.getInfoById(id);
	}
	/**
	 * author BruceZhang
	 * @param id	信息ID
	 * @param comments	要加多少浏览次数
	 * @throws AppBusinessException
	 */
	@Override
	public void updateInfoViews(String id, Integer views)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("views", views);
		infoDao.updateInfoViews(map);
	}
	/**
	 * author BruceZhang
	 * @param id	信息ID
	 * @param comments	要加多少点评量
	 * @throws AppBusinessException
	 */
	@Override
	public void updateInfoComments(String id, Integer comments)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("comments", comments);
		infoDao.updateInfoViews(map);
	}

}
